<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue.js实例</title>
    <style>
        table {
            width: 40%;
            text-align: center;
            margin: 0 auto;
        }

    </style>
</head>

<body>
    <div id="app">
        <table>
            <tr>
                <td>序号</td>
                <td>商品名</td>
                <td>单价</td>
                <td>数量</td>
                <td>小计</td>
            </tr>
            <tr v-for="(item,index) in goods">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td><button v-on:click="sub(index)">-</button><input type="text" v-model="item.number"><button v-on:click="add(index)">+</button></td>
                <td>{{sum(index)}}元</td>
            </tr>
            <tr>
                <td colspan="5">应付：{{count}}元</td>
            </tr>
        </table>
    </div>
</body>
<script src="vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            goods: [
                { id: 1, name: '苹果', price: 4.0, number: 0},
                { id: 2, name: '香蕉', price: 6.0, number: 0},
                { id: 3, name: '雪梨', price: 2.0, number: 0}
            ]
        },
        methods: {
            add:function(e){
                // console.log(e);
                this.goods[e].number++;
            },
            sub:function(e){
                // console.log(e);
                this.goods[e].number--;
            },
            sum:function(index){
                return Math.floor(this.goods[index].price*Number(this.goods[index].number));
            }
        },
        computed: {
            count: function() {
                let sum = 0;
                for(var i=0; i< this.goods.length; i++) {
                    // console.log(i);
                    sum += this.goods[i].price * this.goods[i].number;                    
                }
                return sum;
            }
        }

    })
</script>

</html>